<template>
  <custom-container ref="customContainerRef" :config="mainConfig" />
  <setForm ref="setFormRef" @changeList="handleQuery" />
</template>
<script lang="ts" setup>
import { Delete, Edit, Plus } from '@element-plus/icons-vue'
import { ref, getCurrentInstance, defineAsyncComponent, computed } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { DictDirection } from '@/store/modules/dicts'
import { getCostNameList, addCostNameType, updateCostNameType, deleteCostNameType } from '@/api/BusinessConfig/costNameMain'

const setForm = defineAsyncComponent(() => import('./components/setForm.vue'))
const customContainerRef = ref(null),
  // 查询参数
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
  }),
  setFormRef = ref(null)
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1
  customContainerRef.value.getList()
}
// 新增费用名称
const handleAdd = () => {
  setFormRef.value.open = true
  setFormRef.value.title = '新增费用名称'
}
// 修改费用名称
const handleUpdate = (row: { [key: string]: any }) => {
  const rowList = JSON.parse(JSON.stringify(row))
  rowList.status = rowList.status == 1 ? '生效' : rowList.status == 0 ? '失效' : ''
  setFormRef.value.form = rowList
  setFormRef.value.open = true
  setFormRef.value.title = '修改费用名称'
}
// 删除费用名称
const handleDelete = (row: { [key: string]: any }) => {
  ElMessageBox.confirm(`是否确认删除费用编号为"${ row.id }"的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(() => deleteCostNameType(row.id))
  .then((res) => {
    if (res.code === 200) {
      handleQuery()
      ElMessage.success('删除成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((error) => {
    if (error == 'cancel') {
      ElMessage.error(error)
    }
  })
}

// 主体配置
const mainConfig = computed({
  get() {
    return {
      query: {
        fields: [
          {
            label: '费用名称',
            prop: 'feeName',
          },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            option: {
              placeholder: '请选择',
              dictInfo: {
                dict: DictDirection.通用状态,
              },
            },
          },
        ],
        queryParams: queryParams.value,
      },
      btns: [
        {
          title: '新增',
          icon: Plus,
          directive: [['hasPermi', ['system:role:add']]],
          onClick: handleAdd,
        },
      ],
      table: {
        columns: [
          {
            label: '费用名称',
            prop: 'feeName',
            align: 'center',
            minWidth: '100',
          },
          {
            label: '状态',
            prop: 'status',
            align: 'center',
            autoFormatter: {
              dict: DictDirection.通用状态,
              rowKey: 'status',
            },
          },
          {
            label: '操作',
            align: 'center',
            showOverflowTooltip: false,
            render: (h, params) => {
              const buttons = [
                {
                  title: '修改',
                  icon: Edit,
                  clickFun: handleUpdate,
            
                },
                {
                  title: '删除',
                  icon: Delete,
                  clickFun: handleDelete,
              
                },
              ]
              return buttons.map((el) => h(
                ElButton,
                {
                  text: true,type: 'primary',
                  icon: el.icon,
                  onClick: () => {
                    el.clickFun(params.row)
                  },
                },
                {
                  default: () => el.title,
                },
              ))
            },
          },
        ],
      },
      api: getCostNameList,
    }
  },
  set(val) {
    return val
  },
})
</script>
